package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustomerMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcontract;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcontractMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcustomer;
import com.unlcn.ils.erpInterfaces.service.buss.ISmcontractService;
import com.unlcn.ils.erpInterfaces.service.buss.IWmsOrderLogService;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * SMCONTRACT 合同  服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-08
 */
@Service
public class SmcontractServiceImpl extends ServiceImpl<SmcontractMapper, Smcontract> implements ISmcontractService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmcontractServiceImpl.class);

    @Autowired
    private SmcontractMapper smcontractMapper;

    @Autowired
    private SmcustomerMapper smcustomerMapper;

    @Override
    public Long getContractId(Long customerId) {

        // 查询客户合同
        Wrapper<Smcontract> ew = new EntityWrapper<>();
        ew.eq("ICUSTOMERID", customerId);
        List<Smcontract> smcontractList = smcontractMapper.selectList(ew);

        if (CollectionUtils.isEmpty(smcontractList)) {

            // 查询客户详细信息
            Smcustomer smcustomer = smcustomerMapper.selectById(customerId);
            if (smcustomer == null)
                throw new BaseException(customerId + "：没有对应客户");

            //获取主键值
            Long ilineid = smcontractMapper.getNextIlineid();
            Smcontract smcontract = new Smcontract();
            smcontract.setIlineid(ilineid);
            smcontract.setVccontractno(smcustomer.getVccustomerno());
            smcontract.setVccontractname(smcustomer.getVccustomername()+"运输");
            smcontract.setItype(0L);
            smcontract.setDtdate(new Date());
            smcontract.setDtbegindate(new Date());
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
            try{
                smcontract.setDtenddate(sdf.parse("2019-12-12"));
            }catch (Exception e){
                LOGGER.info("时间转换出错");
            }
            smcontract.setIstatus(1L);
            smcontract.setIcustomerid(smcustomer.getIlineid());
            smcontract.setIcompanyid(13L);
            smcontract.setDtsigndate(new Date());
            smcontract.setBactive(1L);
            smcontract.setVcuserno("system");
            smcontractMapper.insert(smcontract);
            return ilineid;

        } else {
            return smcontractList.get(0).getIlineid();
        }
    }
}
